home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / grafica / amhelios / vector3.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  6KB  |  252 lines

  1. ////////////////////////////////////////////////////////////
  2. //
  3. //  VECTOR3.H - 3-D Vector and Point Classes
  4. //
  5. //  Version:    1.03A
  6. //
  7. //  History:    94/08/23 - Version 1.00A release.
  8. //              94/12/16 - Version 1.01A release.
  9. //              95/02/05 - Version 1.02A release.
  10. //              95/07/21 - Version 1.02B release.
  11. //              96/02/14 - Version 1.02C release.
  12. //              96/04/01 - Version 1.03A release.
  13. //
  14. //  Compilers:  Microsoft Visual C/C++ Professional V1.5
  15. //              Borland C++ Version 4.5
  16. //
  17. //  Author:     Ian Ashdown, P.Eng.
  18. //              byHeart Software Limited
  19. //              620 Ballantree Road
  20. //              West Vancouver, B.C.
  21. //              Canada V7S 1W3
  22. //              Tel. (604) 922-6148
  23. //              Fax. (604) 987-7621
  24. //
  25. //  Copyright 1994-1996 byHeart Software Limited
  26. //
  27. //  The following source code has been derived from:
  28. //
  29. //    Ashdown, I. 1994. Radiosity: A Programmer's
  30. //    Perspective. New York, NY: John Wiley & Sons.
  31. //
  32. //  It may be freely copied, redistributed, and/or modified
  33. //  for personal use ONLY, as long as the copyright notice
  34. //  is included with all source code files.
  35. //
  36. ////////////////////////////////////////////////////////////
  37.  
  38. #ifndef _VECTOR3_H
  39. #define _VECTOR3_H
  40.  
  41. #include <math.h>
  42. #include "general.h"
  43.  
  44. class Vector3;  // Forward reference
  45.  
  46. class Space3    // 3-D co-ordinates
  47. {
  48.   protected:
  49.     float x;    // X-axis co-ordinate
  50.     float y;    // Y-axis co-ordinate
  51.     float z;    // Z-axis co-ordinate
  52.  
  53.   public:
  54.     Space3() { };
  55.     Space3( double xval, double yval, double zval )
  56.     {
  57.       x = (float) xval;
  58.       y = (float) yval;
  59.       z = (float) zval;
  60.     }
  61.  
  62.     double GetX() { return x; }
  63.     double GetY() { return y; }
  64.     double GetZ() { return z; }
  65.  
  66.     void SetX( double xval ) { x = (float) xval; }
  67.     void SetY( double yval ) { y = (float) yval; }
  68.     void SetZ( double zval ) { z = (float) zval; }
  69. };
  70.  
  71. class Point3 : public Space3    // 3-D point
  72. {
  73.   public:
  74.     Point3() : Space3() { };
  75.  
  76.     Point3( double xval, double yval, double zval ) :
  77.         Space3 ( xval, yval, zval )
  78.     { };
  79.  
  80.     // Add vector v to point p
  81.     friend Point3 operator+( Point3 p, Vector3 v );
  82.  
  83.     // Add point p to vector v
  84.     friend Point3 operator+( Vector3 v, Point3 p );
  85.  
  86.     friend class Vector3;
  87. };
  88.  
  89. class Vector3 : public Space3   // 3D vector
  90. {
  91.   public:
  92.     Vector3() : Space3() { };
  93.  
  94.     Vector3( double xval, double yval, double zval ) :
  95.         Space3 ( xval, yval, zval )
  96.     { };
  97.  
  98.     Vector3( Point3 &p ) : Space3()
  99.     { x = p.x; y = p.y; z = p.z; }
  100.  
  101.     Vector3( Point3 &start, Point3 &end ) : Space3()
  102.     {
  103.       x = end.x - start.x;
  104.       y = end.y - start.y;
  105.       z = end.z - start.z;
  106.     }
  107.  
  108.     // Return vector length
  109.     double Length()
  110.     { return sqrt(x * x + y * y + z * z); }
  111.  
  112.     // Assign scalar
  113.     Vector3 &operator=( double s )
  114.     {
  115.       x = (float) s;
  116.       y = (float) s;
  117.       z = (float) s;
  118.       
  119.       return *this;
  120.     }
  121.  
  122.     // Add/assign vector v
  123.     Vector3 &operator+=( Vector3 &v )
  124.     { x += v.x; y += v.y; z += v.z; return *this; }
  125.  
  126.     // Subtract/assign vector v
  127.     Vector3 &operator-=( Vector3 &v )
  128.     { x -= v.x; y -= v.y; z -= v.z; return *this; }
  129.  
  130.     // Multiply/assign by scalar s
  131.     Vector3 &operator*=( double s )
  132.     {
  133.       x *= (float) s;
  134.       y *= (float) s;
  135.       z *= (float) s;
  136.       
  137.       return *this;
  138.     }
  139.  
  140.     // Divide/assign by scalar s
  141.     Vector3 &operator/=( double s )
  142.     {
  143.       x /= (float) s;
  144.       y /= (float) s;
  145.       z /= (float) s;
  146.       
  147.       return *this;
  148.     }
  149.  
  150.     // Negation
  151.     Vector3 operator-()
  152.     {
  153.       Vector3 temp;     // Temporary 3-D vector
  154.  
  155.       temp.x = -x;
  156.       temp.y = -y;
  157.       temp.z = -z;
  158.  
  159.       return temp;
  160.     }
  161.  
  162.     // Add vector v2 to vector v1
  163.     friend Vector3 operator+( Vector3 v1, Vector3 v2 )
  164.     {
  165.       Vector3 temp;     // Temporary 3-D vector
  166.  
  167.       temp.x = v1.x + v2.x;
  168.       temp.y = v1.y + v2.y;
  169.       temp.z = v1.z + v2.z;
  170.  
  171.       return temp;
  172.     }
  173.  
  174.     // Subtract vector v2 from vector v1
  175.     friend Vector3 operator-( Vector3 v1, Vector3 v2 )
  176.     {
  177.       Vector3 temp;     // Temporary 3-D vector
  178.  
  179.       temp.x = v1.x - v2.x;
  180.       temp.y = v1.y - v2.y;
  181.       temp.z = v1.z - v2.z;
  182.  
  183.       return temp;
  184.     }
  185.  
  186.     // Multiply vector v by scalar s
  187.     friend Vector3 operator*( Vector3 v, double s )
  188.     {
  189.       Vector3 temp;     // Temporary 3-D vector
  190.  
  191.       temp.x = v.x * (float) s;
  192.       temp.y = v.y * (float) s;
  193.       temp.z = v.z * (float) s;
  194.  
  195.       return temp;
  196.     }
  197.  
  198.     // Multiply scalar s by vector v
  199.     friend Vector3 operator*( double s, Vector3 v )
  200.     { return v * s; }
  201.  
  202.     // Divide vector v by scalar s
  203.     friend Vector3 operator/( Vector3 v, double s )
  204.     {
  205.       Vector3 temp;     // Temporary 3-D vector
  206.  
  207.       temp.x = v.x / (float) s;
  208.       temp.y = v.y / (float) s;
  209.       temp.z = v.z / (float) s;
  210.  
  211.       return temp;
  212.     }
  213.  
  214.     // Divide scalar s by vector v
  215.     friend Vector3 operator/( double s, Vector3 v )
  216.     { return v / s; }
  217.  
  218.     // Normalize
  219.     Vector3 &Norm()
  220.     {
  221.       double len = Length();
  222.  
  223.       if (len < MIN_VALUE)
  224.         len = 1.0;
  225.         
  226.       x /= (float) len;
  227.       y /= (float) len;
  228.       z /= (float) len;
  229.  
  230.       return *this;
  231.     }
  232.  
  233.     // Return dot product of vectors v1 and v2
  234.     friend double Dot( Vector3 &v1, Vector3 &v2 )
  235.     { return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z); }
  236.  
  237.     // Return cross product of vectors v1 and v2
  238.     friend Vector3 Cross( Vector3 &v1, Vector3 &v2 )
  239.     {
  240.       Vector3 temp;     // Temporary 3-D vector
  241.  
  242.       temp.x = v1.y * v2.z - v1.z * v2.y;
  243.       temp.y = v1.z * v2.x - v1.x * v2.z;
  244.       temp.z = v1.x * v2.y - v1.y * v2.x;
  245.  
  246.       return temp;
  247.     }
  248. };
  249.  
  250. #endif
  251.  
  252.